/*
	Copyright 2009 Omer Saatcioglu
	Licensed under the Apache License, Version 2.0 (the "License");
	you may not use this file except in compliance with the License.
	You may obtain a copy of the License at

		http://www.apache.org/licenses/LICENSE-2.0

	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.

	$Author$
	$Revision$
	$LastChangedDate$
   
 */

package com.saatcioglu.android.guessthenumber;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

public class GfxMasterShot extends GfxGuesstheNumber {
	
	boolean isEndoftheGame = false;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		CtrlMaintanence.getInstance().getCurrentLevel();
		if(SMBGuesstheNumber.nCurrentLevel >= SMBGuesstheNumber.MASTER_LEVEL_NUMBER){
			showDialog(R.id.display_endofgame);
			super.onCreate(savedInstanceState);
			numberConsole.reset();
			isEndoftheGame = true;
			return;
		}
		updateMasterSettings();
		super.onCreate(savedInstanceState);
		if (SMBGuesstheNumber.isGameStarted) {
			resumetheGame();
		}
		else{
			removeDialog(R.id.display_start);
			showDialog(R.id.display_start);
		}
	}

	@Override
	protected void preparePreference() {
		SMBGuesstheNumber.gameSettings = getSharedPreferences(
				CtrlMaintanence.MASTER_SETTINGS_NAME, 0);
		Log.d("GfxMasterShot::preparePreference", "enter");
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		Log.d("GfxMasterShot::onCreateDialog", "enter");
		String message = new String();
		switch (id) {
		case R.id.display_start:
			message = formatLevel()
			+ formatMission();
			return new AlertDialog.Builder(this)
			.setIcon(R.drawable.droid)
			.setTitle(R.string.menu_levelshot)
			.setMessage(message)
			.setPositiveButton(R.string.alert_dialog_newgame, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog,	int whichButton) {
				}
			})
			.create();
		case R.id.display_success:
			Log.d("GfxMasterShot::onCreateDialog", "Succcess " +  Integer.toString(SMBGuesstheNumber.nCurrentLevel));
			message = "You have mastered to a new level" 
			+ "\nNext " + formatLevel()
			+ formatMission()
			+ "\n\nTip: If you think this is good, you can play this level again without loosing your level advance";
			return new AlertDialog.Builder(this)
			.setIcon(R.drawable.smiley_happy)
			.setTitle(R.string.dialog_success)
			.setMessage(message)
			.setPositiveButton(R.string.alert_dialog_newgame, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog,	int whichButton) {
					StartaNewGame();
				}
			})
			.setNegativeButton(R.string.alert_currentlevel, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int whichButton) {
					updateMasterSettings(--SMBGuesstheNumber.nCurrentLevel);
					removeDialog(R.id.display_start);
					showDialog(R.id.display_start);
					StartaNewGame();
				}
			})
			.create();
		case R.id.display_failure:
			message = "Try again! You can do it\n" 
			+ formatLevel()
			+ formatMission()
			+ "\n\nTip: If you think of mercy, you can play a lower level for a while without loosing your level advance";
			return new AlertDialog.Builder(this)
			.setIcon(R.drawable.smiley_sad)
			.setTitle(R.string.dialog_failure)
			.setMessage(message)
			.setPositiveButton(R.string.alert_dialog_newgame, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int whichButton) {
					StartaNewGame();
				}
			})
			.setNegativeButton(R.string.alert_previouslevel, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog,	int whichButton) {
					updateMasterSettings(--SMBGuesstheNumber.nCurrentLevel);
					removeDialog(R.id.display_start);
					showDialog(R.id.display_start);
					StartaNewGame();
				}
			})
			.create();
		case R.id.display_endofgame:
			message = "Horayyy!! You have reached to the end of the world..."; 
			return new AlertDialog.Builder(this)
			.setIcon(R.drawable.smiley_happy)
			.setTitle(R.string.dialog_success)
			.setMessage(message)
			.setPositiveButton(R.string.alert_dialog_endofgame, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog,	int whichButton) {
					finish();
				}
			})
			.create();
		}
		return super.onCreateDialog(id);
	}
	
	private String formatLevel(){
		String message = new String();
		message = "Level: " + CtrlMaintanence.getInstance().formatLevels(SMBGuesstheNumber.nCurrentLevel); 
		message += "\n";
		return message;
	}
	protected String _formatMissionTimeMasterShot(){
		return "\nNo time limit";
	}
	protected String _formatMissionTimeandMaxMasterShot(){
		return "\nNo time and shot limit";
	}
	
	private void setMasterLevel(int intLevel){
		int temp = SMBGuesstheNumber.nCurrentLevel;
		CtrlMaintanence.getInstance().getCurrentLevel();
		if(intLevel >= SMBGuesstheNumber.nCurrentLevel){
			CtrlMaintanence.getInstance().setCurrentLevel(intLevel);
			SMBGuesstheNumber.nCurrentLevel = intLevel;
		} else{
			SMBGuesstheNumber.nCurrentLevel = temp;
		}
	}
	
	private void updateMasterSettings(){
		Log.d("GfxMasterShot::updateMasterSettings", "enter0");
		updateMasterSettings(SMBGuesstheNumber.nCurrentLevel);
	}
	
	private void updateMasterSettings(int intLevel){
		Log.d("GfxMasterShot::updateMasterSettings", "enter1");
		SMBGuesstheNumber.nActiveDigits = SMBGuesstheNumber.arrLevels[intLevel][0];
		SMBGuesstheNumber.nActiveMaxShots = SMBGuesstheNumber.arrLevels[intLevel][1];
		SMBGuesstheNumber.nActiveMaxTime = SMBGuesstheNumber.arrLevels[intLevel][2];
		Log.d("GfxMasterShot::updateGameSettings", "current nCurrentLevel: " + Integer.toString(intLevel));
		Log.d("GfxMasterShot::updateGameSettings", "nActiveDigits: " + Integer.toString(SMBGuesstheNumber.nActiveDigits));	
		Log.d("GfxMasterShot::updateGameSettings", "nActiveMaxShots: " + Integer.toString(SMBGuesstheNumber.nActiveMaxShots));	
		Log.d("GfxMasterShot::updateGameSettings", "nActiveMaxTime: " + Integer.toString(SMBGuesstheNumber.nActiveMaxTime));	
	}

	protected void gameSucceded() {
		super.gameSucceded();
		++SMBGuesstheNumber.nCurrentLevel;
		setMasterLevel(SMBGuesstheNumber.nCurrentLevel);
		if(SMBGuesstheNumber.nCurrentLevel < SMBGuesstheNumber.MASTER_LEVEL_NUMBER)
		{
			updateMasterSettings();
			removeDialog(R.id.display_success);
			showDialog(R.id.display_success);
		}
		else
		{
			showDialog(R.id.display_endofgame);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		if(isEndoftheGame){
			finish();
		}
		menu.add(0, ABOUT_ID, ABOUT_ID, R.string.about);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case ABOUT_ID:
			showDialog(R.id.display_hints);
			//startActivityForResult(new Intent(this, GfxLevelsList.class), R.id.menu_customsettings);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case R.id.menu_customsettings:
			if (SMBGuesstheNumber.isPreferencesChanged) {
				StartaNewGame();
			}
		}
	}
}
